define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/audit/index' + location.search,
                    //add_url: 'shop/audit/add',
                    edit_url: 'shop/audit/edit',
                    del_url: 'shop/audit/del',
                    multi_url: 'shop/audit/multi',
                    //import_url: 'shop/shop/import',
                    table: 'shop',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                $("input[name='industry_id']", form).addClass("selectpage")
                    .data("source", "industry/index")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true,
                            formatter:function(value, row, index){
                                if (row.status != 1){
                                    return {disabled: true};
                                }
                            }
                        },
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'type', title: __('Type'), formatter: Table.api.formatter.flag, searchList: typeList},
                        {field: 'industry_id', title: __('Industry_id'), visible: false},
                        {field: 'industry_name', title: __('Industry_id'), operate: false},
                        {field: 'phone_name', title: __('Phone_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        //{field: 'expiration_time', title: __('Expiration_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'),
                            formatter: function(value, row, index){
                                if(value == 4){
                                    return "<span style='color: red;'>已过期</span>";
                                }else if(value == 3){
                                    return "审核拒绝， 拒绝原因:"+ row.remark;
                                }else if(value == 2){
                                    return "审核通过， 过期时间:"+ row.expiration_time_text;
                                }else{
                                    return "审核中";
                                }
                            },
                            searchList: statusList,
                            cellStyle: function(value, row, index, filed){
                                return {
                                    css : {
                                        "white-space": 'normal',
                                        //"text-overflow": 'ellipsis',
                                        //"overflow": 'hidden',
                                        "word-break": 'break-all',
                                        "max-width": '200px'
                                    }
                                }
                            },
                            //operate: false
                        },
                        //{field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'apply_time', title: __('Apply_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'audit',
                                    icon: 'fa fa-check',
                                    title: '审核',
                                    text: '审核',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'shop/audit/audit',
                                    hidden: function(row, value, index){
                                        if(row.status == 1){
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                                {
                                    name: 'edit',
                                    icon: 'fa fa-pencil',
                                    title: '编辑',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    hidden: function(row, value, index){
                                        if(row.status == 2){
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                                {
                                    name: 'del',
                                    icon: 'fa fa-trash',
                                    title: '删除',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-danger btn-delone',
                                    hidden: function(row, value, index){
                                        if(row.status == 1){
                                            return false;
                                        }
                                        return true;
                                    },
                                    confirm: "确定删除此项?"
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index_audit: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/audit/index_audit' + location.search,
                    edit_url: 'shop/audit/edit',
                    table: 'shop',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'audit_time',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'type', title: __('Type'), formatter: Table.api.formatter.flag, searchList: typeList},
                        {field: 'phone_name', title: __('Phone_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'expiration_time', title: __('Expiration_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: function(value, row, index){
                            if(row.status == 4){
                                return "<span style='color: red;'>已过期</span>";
                            }else if(row.status == 3){
                                return "";
                            }else if(row.status == 2){
                                return row.expiration_time_text;
                            }else{
                                return "";
                            }
                        }},
                        //{field: 'status', title: __('Status'),
                        //    formatter: function(value, row, index){
                        //        if(value == 4){
                        //            return "<span style='color: red;'>已过期</span>";
                        //        }else if(value == 3){
                        //            return "审核拒绝， 拒绝原因:"+ row.remark;
                        //        }else if(value == 2){
                        //            return "审核通过， 过期时间:"+ row.expiration_time_text;
                        //        }else{
                        //            return "审核中";
                        //        }
                        //    },
                        //    searchList: statusList,
                        //    cellStyle: function(value, row, index, filed){
                        //        return {
                        //            css : {
                        //                "white-space": 'normal',
                        //                //"text-overflow": 'ellipsis',
                        //                //"overflow": 'hidden',
                        //                "word-break": 'break-all',
                        //                "max-width": '200px'
                        //            }
                        //        }
                        //    }
                        //},
                        {field: 'user.status', title: __('禁用状态'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('禁用')}},
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'apply_time', title: __('Apply_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index_audit_not: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/audit/index_audit' + location.search,
                    table: 'shop',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'type', title: __('Type'), formatter: Table.api.formatter.flag, searchList: typeList},
                        {field: 'phone_name', title: __('Phone_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),
                            formatter: function(value, row, index){
                                if(value == 4){
                                    return "<span style='color: red;'>已过期</span>";
                                }else if(value == 3){
                                    return "审核拒绝， 拒绝原因:"+ row.remark;
                                }else if(value == 2){
                                    return "审核通过， 过期时间:"+ row.expiration_time_text;
                                }else{
                                    return "审核中";
                                }
                            },
                            //searchList: statusList,
                            cellStyle: function(value, row, index, filed){
                                return {
                                    css : {
                                        "white-space": 'normal',
                                        //"text-overflow": 'ellipsis',
                                        //"overflow": 'hidden',
                                        "word-break": 'break-all',
                                        "max-width": '200px'
                                    }
                                }
                            }
                        },
                        //{field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'apply_time', title: __('Apply_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        audit: function () {
            Controller.api.bindevent();
            $(document).on('click', 'input[name="status"]', function(){
                let status = $(this).val();
                if(status == 3){
                    if($('.remark').hasClass('hide')){
                        $('.remark').removeClass('hide');
                    }
                    if(!$('.expiration_time').hasClass('hide')){
                        $('.expiration_time').addClass('hide');
                    }
                }else{
                    if(!$('.remark').hasClass('hide')){
                        $('.remark').addClass('hide');
                    }
                    if($('.expiration_time').hasClass('hide')){
                        $('.expiration_time').removeClass('hide');
                    }
                }
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});